<?php
use app\models\Mall;
use yii\helpers\Url;

$web = Url::base();
$integral_unit = \app\models\Mall::getCurrentInstitutionIntegralUnit();
$site_name = \app\models\Mall::getCurrentInstitutionSiteName();
$institution_code = Mall::getCurrentInstitutionCode();
$this->title = $site_name . '->商品详情';
?>

<script src="<?= $web ?>/js/mall-m/parabola.js"></script>

<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/goods-detail.css"/>
<link rel="stylesheet" type="text/css" href="<?= $web ?>/css/mall-m/cross-title.css"/>
<style type="text/css">
    .back-top {
        background-image: url('<?= $web ?>/images/mall-m/index-icon-top.png');
    }
</style>

<div class="main">
    <div class="content">
        <div class="middle-banner">
            <div class="swiper-container">
                <div class="swiper-wrapper">
                    <?php
                    foreach ($goods['goods_image_list'] as $item) {
                        ?>
                        <div class="swiper-slide">
                            <img src="<?= $item['image_url'] ?>"/>
                        </div>
                        <?php
                    }
                    ?>
                    <!-- <div class="swiper-slide">
                        <img src="<? /*= $web */ ?>/images/mall-m/banner1-img.png"/>
                    </div>-->
                </div>
                <div class="swiper-pagination"></div>
            </div>
        </div>
        <script language="javascript">
            var mySwiper = new Swiper('.swiper-container', {
                pagination: '.swiper-pagination'
            })
            function goBack() {
                //alert(document.referrer);
                // history.back();
                history.go(-1);
            }
        </script>
        <a href="javascript:void(0);" class="go-back" onclick="goBack()">
            <img src="<?= $web ?>/images/mall-m/go-back2.png"/>
        </a>

        <div class="goods-content">
            <p class="goods-title f-l crossborder">
                <?php
                if ($goods['is_crossborder'] == '1') {
                    ?>
                    <span>跨境</span>
                    <?php
                }
                ?>
                <?= $goods['name'] ?></p>
            <a href="" style="display: none;">
                <div class="collect-btn f-r">
                    <img class="f-l" src="<?= $web ?>/images/mall-m/collect-btn.png"/>
                    <p class="f-l">收藏</p>
                </div>
            </a>
            <p class="number">产品编号：<span><?= $goods['code'] ?></span></p>
            <?php
            if ($goods['is_activity'] == '1') {
                ?>
                <p class="convert f-l">原价：<span class="integration-1"><s><?= intval($goods['old_price']) ?><span
                                class="text"><?= $integral_unit ?></span></s></span></p>
                <?php
            }
            ?>
            <p class="convert f-l">兑换：<span class="integration"><?= intval($goods['selling_integral']) ?><span
                        class="text"><?= $integral_unit ?></span></span></p>

            <p class="inventory f-r">库存：<span>正在获取..</span></p>
        </div>
        <?php
        if (count($goods['group_goods_list']) > 1) {
            ?>
            <div class="model">
                <div class="model-ul">
                    <?php
                    foreach ($goods['group_goods_list'] as $item) {
                        $group_item_name = $item['group_item_name'];
                        if ($item['code'] == $goods['code']) {
                            echo "<a class='model-li model-li-selected' href='#'>$group_item_name</a>";
                        } else {
                            $other_goods_code = $item['code'];
                            echo "<a class='model-li' href='goods-detail?goods_code=$other_goods_code&institution_code=$institution_code'>$group_item_name</a>";
                        }
                    }
                    ?>

                    <!--                <a class='model-li model-li-selected'>灰色，39.5</a>-->
                    <!--                <a class="model-li">灰色，39.5</a>-->

                </div>
            </div>
            <?php
        }
        ?>
        <div class="equity">
            <div class="equity-title f-l">
                <img class="f-l" src="<?= $web ?>/images/mall-m/equity-icon.png"/>
                <p class="f-l">全场包邮</p>
            </div>
            <div class="equity-title f-l">
                <img class="f-l" src="<?= $web ?>/images/mall-m/equity-icon.png"/>
                <p class="f-l">正品保障</p>
            </div>
            <div class="equity-title f-l">
                <img class="f-l" src="<?= $web ?>/images/mall-m/equity-icon.png"/>
                <p class="f-l">7天退货</p>
            </div>
            <div class="equity-title f-l">
                <img class="f-l" src="<?= $web ?>/images/mall-m/equity-icon.png"/>
                <p class="f-l">12小时发货</p>
            </div>
        </div>
        <?php
        if ($goods['is_crossborder'] == '1') {
            ?>
            <div class="cross-border">
                <p class="title">跨境保税商品购买须知</p>
                <p>一、什么是跨境保税商品</p>
                <p>
                    跨境保税商品是采购自国外的商品，进境时海关、国检都会对商品的品质做审查（品质保证），进境后存放于保税区仓库，国内消费者通过电子商务平台下单购买后，在海关全程监管下打包出区，最终交由国内快递送至消费者手上。优点：商品进境时海关、国检对商品审查，商品存放保税仓库（绝无假货，品质保证）</p>
                <p>二、购买跨境保税商品要求</p>
                <p>1、单笔订单不超过<span>2000元</span>人民币；</p>
                <p>2、每人次一年不能购买超过2万元人民币跨境保税商品（按身份证号计算）；</p>
                <p>3、根据中国海关总署要求，您所购买的商品需提供<span>身份证信息</span>进行入境申报，请您配合。我们不会向第三方泄露您的资料，请您放心。</p>
                <p>三、跨境商品物流配送</p>
                <p>跨境产品由于海关检验的特殊性，物流送达时间一般时效为：</p>
                <p>保税区邮：<br/>保税区—圆通，时效<span>5-7天</span></p>
                <p>香港直邮：<br/>香港外包仓—申通快递，时效<span>10天左右</span><br/>香港汇通仓—汇通快递，时效<span>8-10天</span><br/>香港—圆通，时效<span>10-12天</span>
                </p>
                <p>
                    海外直邮：<br/>日本直邮—EMS，时效<span>5-7天</span><br/>日本—国际段联邦国内转邮政小包或韵达，时效<span>10-13天</span><br/>荷兰—中欧快运，时效<span>10-15天</span>
                </p>
<!--                <div class="reminder">-->
<!--                    <p class="reminder-name">温馨提示：</p>-->
<!--                    <p class="reminder-text">由于海关系统升级于<span>16年12月30日18:00</span>后兑换的跨境产品，需要<span>17年1月10日</span>开始发货，给您带来的不变敬请谅解。</p>-->
<!--                </div>-->
            </div>
            <?php
        }
        ?>

        <div class="dragging">
            <p>继续拖动，查看图文详情</p>
        </div>
    </div>

    <div class="page-2">
        <div class="goods-details">
            <div class="details-title">
                <p>详情信息</p>
            </div>
            <div class="details-content">
                <?= $goods['detail_html'] ?>
            </div>
        </div>
        <div class="history">
            <p class="f-l">历史足迹</p>
            <img class="f-l" src="<?= $web ?>/images/mall-m/history-icon.png"/>
        </div>
        <div class="goods-container">
            <div class="goods-grid-frame" id="his_goods_grid">
                <!--                --><?php
                //                    foreach($hotGoosList as $item){
                //                        ?>
                <!--                        <a class="goods-item" href="goods-detail?goods_code=-->
                <? //= $item['code'] ?><!--">-->
                <!--                            <img src="--><? //=$item['image_url']?><!--">-->
                <!--                            <label class="name">--><? //=$item['name']?><!--</label>-->
                <!--                            <label class="price">--><? //=$item['integral_unit']?><!--：-->
                <? //=intval($item['selling_integral'])?><!--</label>-->
                <!--                        </a>-->
                <!--                        --><?php
                //                    }
                //                ?>
                <!-- <a class="goods-item" href="#">
                     <img src="">
                     <label class="name">111</label>
                     <label class="price">111：222</label>
                 </a>-->

            </div>
        </div>
    </div>

    <div class="back-top"></div>

    <div class="shop-car-btn">
        <a href="<?= Url::to(['mall-m/cart', 'institution_code' => Mall::getCurrentInstitutionCode()]) ?>">
            <div class="shop-car f-l">
                <img src="<?= $web ?>/images/mall-m/shop-car.png"/>
                <p id="target" class="shop-car-number"><?= $cartGoodsCount ?></p>
            </div>
        </a>
        <a href="javascript:void(0)">
            <div id="setOptions" class="void f-l">
                <p>加入购物车</p>
                <div id="boll" class="boll"></div>
            </div>
        </a>

    </div>
</div>
<script type="text/javascript">

    var bool = new Parabola({
        el: "#boll"
    });
    var animate_event = function () {
        $(".boll").show();
        bool.setOptions({
            targetEl: $("#target"),
            curvature: 0.01,
            duration: 400
        });
        bool.start();
        $.fun1();
    };

    $.extend({
        'fun1': function () {
            var timer = setInterval(function () {
                var n = $(".shop-car-number").html();
                var num = parseInt(n) + 1;
                $(".shop-car-number").html(num);
                $(".shop-car-number").addClass("animate1");
                clearInterval(timer);
                $.fun2();
            }, 400);
        }
    });
    $.extend({
        "fun2": function () {
            var timer2 = setInterval(function () {
                $(".shop-car-number").removeClass("animate1");
                clearInterval(timer2);
            }, 400);
        }
    });

</script>
<script type="application/javascript">
    $(function () {
        $(document).scroll(function () {
            var scrollTop = $(document).scrollTop();
            if (scrollTop > 500) {
                $('.back-top').show(500);
            } else {
                $('.back-top').hide(500);
            }
        });

        $('.back-top').click(function () {
            $('body,html').animate({scrollTop: 0}, 200);
        });
//        $('body,html').scrollTop(0);

        $('.details-content').each(function () {
            new RTP.PinchZoom($(this), {});
        });
//        myStorage.clear();
        //展示历史足迹
        if (myStorage) {
            //判断是否同一机构
            var institutionCode = '<?=$institutionCode?>';
            if (myStorage.institution_code) {
                if (myStorage.institution_code != institutionCode) {
                    myStorage.clear();
                }

            } else {
                myStorage.institution_code = institutionCode;
            }

            if (myStorage.his_goods_array) {
                var his_goods_array = JSON.parse(myStorage.his_goods_array);
                var data = {};
                data.goods_code_list = his_goods_array;
                requestJsonMallM('get-his-goods-handler', data, true, function (response) {
                    if (response.status == 0) {
                        var goods_list = response.data;
                        var html = '';
                        for (var j = 0; j < his_goods_array.length; j++) {
                            for (var i = 0; i < goods_list.length; i++) {
                                if (his_goods_array[his_goods_array.length - 1 - j] == goods_list[i].code) {
                                    html += '<a class="goods-item" href="goods-detail?goods_code=' + goods_list[i].code + '&institution_code=<?=$institution_code?>">';
                                    html += '<img src="' + goods_list[i].image_url + '@300w">';
                                    var crossborder_span = '';
                                    if (goods_list[i].is_crossborder == 1) {
                                        crossborder_span = '<span>跨境</span>';
                                    }
                                    html += '<label class="name crossborder">' + crossborder_span + goods_list[i].name + '</label>';
                                    html += '<label class="price">' + goods_list[i].integral_unit + ':' + parseInt(goods_list[i].selling_integral) + '</label>';
                                    html += '</a>';
                                    break;
                                }
                            }
                        }
                        $("#his_goods_grid").append(html);
                    }
                });
            }
        }

        //添加当前商品访问记录,显示10个
        if (myStorage) {
            var goods_code = "<?=$goods['code']?>";
            if (goods_code) {
                var his_goods_array = [];
                var new_goods_array = [];
                if (myStorage.his_goods_array != undefined) {
                    his_goods_array = JSON.parse(myStorage.his_goods_array);
                }

                if (his_goods_array.length > 0) {
                    //只显示9个
                    var temp_array = [];
                    var j = 0;
                    for (var i = 0; i < his_goods_array.length; i++) {
                        if (his_goods_array[his_goods_array.length - 1 - i] != goods_code) {
                            temp_array.push(his_goods_array[his_goods_array.length - 1 - i]);
                            j = j + 1;
                            if (j == 9) {
                                break;
                            }
                        }
                    }

                    for (var i = 0; i < temp_array.length; i++) {
                        new_goods_array.push(temp_array[temp_array.length - 1 - i]);
                    }
                }

                new_goods_array.push(goods_code);
                myStorage.his_goods_array = JSON.stringify(new_goods_array);
            }
        }

        getGoodsStock();
    });

    //删除历史足迹
    function delHisNoStockGoods(goods_code) {
        var his_goods_array = [];
        if (myStorage.his_goods_array != undefined) {
            his_goods_array = JSON.parse(myStorage.his_goods_array);
            his_goods_array.remove(goods_code);
        }
    }

    function addCart(goods_code) {
        var data = {};
        data.goodsCode = goods_code;
        data.quantity = 1;
        var re = requestJsonMallM('add-cart-handle', data);
        if (re.status == 0) {
            // alert('添加成功');
            animate_event();
        } else {
            alert(re.message);
        }
    }

    function getGoodsStock() {
        var goods_code = '<?= $goods['code'] ?>';
        requestJson('get-goods-stock-handle', {'goodsCode': goods_code}, true, function (re) {
            var stock = re.data;
            var stockTxt = '';
//            stock=0;
            if (<?=$goods['is_shelved'] == '0' || $goods['is_show'] == '0' || $goods['is_enabled'] == '0' ? 'true' : 'false'?>) {
                $('.inventory span').html('商品不存在');
                delHisNoStockGoods(goods_code);
                return;
            }
            if (stock <= 0) {
                stockTxt = '缺货';
                delHisNoStockGoods(goods_code);
            } else {


                if (stock >= 20) {
                    stockTxt = '库存充足';
                } else {
                    stockTxt = '库存紧张';
                }
                $('#setOptions').removeClass('void').addClass('join-btn');
                $('#setOptions').closest('a').attr('href', "javascript:addCart('<?= $goods['code'] ?>')");
            }

            $('.inventory span').html(stockTxt);

        });
    }
</script>
